// pages/info/info.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    width: 0,
    height: 0,
    hidden: true,
    hidden1: true,
    code: "",
    encry: "",
    iv: "",
    rawData: "",
    signature: "",
    phoneNumber: "",
    pay: "",
    code: ""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  login: function (e) {
    var that = this
    wx.login({
      success: function (res) {
        if (res.code) {
          console.log(res)
          //发起网络请求
          wx.request({
            url: 'https://weixin.dingzhuobo.com/wxa/user/login',
            data: {
              code: res.code
            },
            success: function (res) {
              console.log(res)
              var data = res.data;
              if (data.errcode !== 500) {
                wx.setStorageSync("sessionId", data.sessionId);

                // resolve(res);
              } else {
                reject(data);
              }
            }
          })
          that.setData({
            code: res.code,
            hidden: false
          })
        } else {
          console.log('获取用户登录态失败！' + res.errMsg)
        }
      }
    });
  },
  getUserInfo: function (e) {
    var session = wx.getStorageSync("sessionId")
    console.log("######################")
    console.log(session)
    var that = this
    wx.getUserInfo({
      success: function (res) {
        console.log(res)
        that.setData({
          hidden1: false,
          encry: res.encryptedData,
          iv: res.iv,
          rawData: res.rawData,
          signature: res.signature,
        })
        wx.request({
          url: 'https://weixin.dingzhuobo.com/wxa/user/info',
          data: {
            rawData: res.rawData,
            signature: res.signature,
            encryptedData: res.encryptedData,
            iv: res.iv
          },
          header: {
            'wxa-sessionid': wx.getStorageSync("sessionId")
          },
          success: function (res) {
            console.log(res)
            // resolve(res);
          }
        })
      },
      fail: function (e) {
      }
    })
  },
  getPhoneNumber: function (res) {
    console.log(res)
    var that = this
    if (res.detail.errMsg == "getPhoneNumber:ok") {
      var session = wx.getStorageSync("sessionId")
      // console.log(e.detail.errMsg)
      // console.log(e.detail.iv)
      // console.log(e.detail.encryptedData)
      wx.request({
        url: 'https://weixin.dingzhuobo.com/wxa/user/phone',
        data: {
          encryptedData: res.detail.encryptedData,
          iv: res.detail.iv
        },
        header: {
          'wxa-sessionid': wx.getStorageSync("sessionId")
        },
        success: function (res) {
          console.log(res)
          // resolve(res);
          that.setData({
            phoneNumber: res.data.phoneNumber
          })
        }
      })
    }
    else {
      that.setData({
        phoneNumber: "用户取消了授权"
      })
    }
  }, pay: function (e) {
    var that = this
    wx.request({
      url: 'https://weixin.dingzhuobo.com/wxa/pay',
      data: {
        body: "车费",
        totalFee: 1,
      },
      header: {
        'wxa-sessionid': wx.getStorageSync("sessionId")
      },
      success: function (res) {
        console.log(res)
        wx.requestPayment({
          'timeStamp': res.data.timeStamp,
          'nonceStr': res.data.nonceStr,
          'package': res.data.package,
          'signType': 'MD5',
          'paySign': res.data.paySign,
          success: function (res) {
            that.setData({
              pay: "支付成功"
            })
          },
          fail: function (res) {
            console.log('fail:' + JSON.stringify(res));
            that.setData({
              pay: "支付取消"
            })
          }
        })
      },
      fail: function (err) {
        console.log(err)
      }
    })
  },
})